--[[
    背包格子类
]]
local bagItem = BaseClass("bagItem")

function bagItem : __init(bagItem, goodDate, bagPanel)
    -- 查找组件
    self.bagItem = bagItem
    self.goodDate = goodDate
    self.bagPanel = bagPanel
    self.icon = self.bagItem.transform : Find("Icon") : GetComponent("Image")
    self.countText = self.bagItem.transform : Find("Count") : GetComponent("Text")

    -- 赋值
    local sprite = Resources.Load("Icon/".. self.goodDate.icon, typeof(CS.UnityEngine.Sprite))
    self.icon.sprite = sprite
    self : RefreshCountText(self.goodDate.count)

    -- 添加点击事件
    self : AddClickEvent()
end

-- 添加点击事件
function bagItem : AddClickEvent()
    local eventTrigger = self.bagItem : GetComponent(typeof(CS.UnityEngine.EventSystems.EventTrigger))
    if not eventTrigger then
        eventTrigger = self.bagItem : AddComponent(typeof(CS.UnityEngine.EventSystems.EventTrigger))
    end
    local entry = CS.UnityEngine.EventSystems.EventTrigger.Entry()
    entry.eventID = CS.UnityEngine.EventSystems.EventTriggerType.PointerClick
    entry.callback : AddListener(function(eventData)
        self : OnClick(eventData) 
    end)

    eventTrigger.triggers : Add(entry)
end

function bagItem : OnClick()
    Global.SelectBagItemDate = self.goodDate
    print("BgaItem输出:点击物品".. Global.SelectBagItemDate.name)
    -- 生成详情面板
    local detailPanel = GameObject.Instantiate(Resources.Load("UIPanel/RightBackgroundPanel"), self.bagPanel.transform)
    if detailPanel ~= nil then
        -- 查找组件
        local detailIcon = detailPanel.transform : Find("Icon") : GetComponent("Image")
        local detailName = detailPanel.transform : Find("Name") : GetComponent("Text")
        local detailDescription = detailPanel.transform : Find("Description") : GetComponent("Text")
        -- 赋值
        detailIcon.sprite = Resources.Load("Icon/".. Global.SelectBagItemDate.icon, typeof(CS.UnityEngine.Sprite))
        detailName.text = "名称:"..Global.SelectBagItemDate.name
        detailDescription.text = "介绍\n"..(Global.SelectBagItemDate.description)
    else
        print("BgaItem输出:生成详情面板失败")
    end
end

-- 刷新物品数量文本
function bagItem : RefreshCountText(count)
    self.countText.text = "x"..count
end

return bagItem